//
//  MainTabViewController.swift
//  ZTC_Final
//
//  Created by xy Man on 2022/5/26.
//

import UIKit

class MainTabViewController: UITabBarController {

    var username:String?
    override func viewDidLoad() {
        super.viewDidLoad()
        print(username!)
        initShoppingDB()
        initHistoryDB()
//        testHistoryDB()
        let totalNUm = getAllNum()
        // 将用户信息传递到子VC中
        let nav1 = self.viewControllers![0] as?UINavigationController
        
        let bookVC = nav1?.viewControllers.first as! BookTableViewController
        bookVC.username = username
        
        let nav2 = self.viewControllers![1] as? UINavigationController
        nav2?.tabBarItem.badgeValue = "\(totalNUm)"
        let shoppingVC = nav2?.viewControllers.first as!ShoppingTableViewController
        shoppingVC.username = username
        
        
        let nav3 = self.viewControllers![2] as? UINavigationController
        let historyVC = nav3?.viewControllers.first as! HistoryTableViewController
        historyVC.username = username!
        // Do any additional setup after loading the view.
    }
    
    func initShoppingDB()->Void{
        // 初始化数据库
        let sqlite = SQLiteManager.sharedInstance
        
        if !sqlite.openDB(){return}
        //创建书籍信息表
        let createShoppingTable = "CREATE TABLE IF NOT exists shopping('username'  text, 'bookname' text, 'number' integer, PRIMARY KEY('username', 'bookname'));"
        if !sqlite.execNoneQuerySQL(sql: createShoppingTable){sqlite.closeDB(); return}
        else{
            print("购物车表初始化成功")
        }
    }
    
    func initHistoryDB()->Void{
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let createHistory = "CREATE TABLE IF NOT exists history(id integer, username text, bookname, number integer, time text, totalprice double, PRIMARY KEY(username, bookname, time));"
        if !sqlite.execNoneQuerySQL(sql: createHistory){return}
        else{
            print("历史记录表初始化成功")
        }
        sqlite.closeDB()
    }
    func getQueryData(sql:String)->[[String : AnyObject]]?{
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return []}
        let queryres = sqlite.execQuerySQL(sql: sql)
        sqlite.closeDB()
        return queryres
    }
    
    func doNonSql(sql:String)->Void{
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        if !sqlite.execNoneQuerySQL(sql: sql){return}
        sqlite.closeDB()
    }
    
    func testHistoryDB()->Void{
        let sql1 = "SELECT * FROM history;"
        let res1 = getQueryData(sql: sql1)
        let sql2 = "INSERT INTO history VALUES(1, 'ztc', 'aaa', 10, '2022-5-27', 9.88);"
        let sql3 = "INSERT INTO history VALUES(2, 'cmq', 'bbb',20,'2022-5-27', 9.99);"
        let sql4 = "SELECT MAX(id) FROM history;"
        doNonSql(sql: sql2)
        let res2 = getQueryData(sql: sql1)
        doNonSql(sql: sql3)
        let res3 = getQueryData(sql: sql1)
        let res4 = getQueryData(sql: sql4)
        print(res1)
        print(res2)
    }
    
    func getAllNum()->Int{
        var totalNum = 0
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return 9999}
        let sqlAllBookName = "SELECT title FROM book;"
        let allBookName = sqlite.execQuerySQL(sql: sqlAllBookName)
        for name in allBookName!{
            totalNum = totalNum + getSingleNum(username: username!, bookname: name["title"] as! String)
        }
        sqlite.closeDB()
        return totalNum
    }
    
    func getSingleNum(username:String, bookname:String)->Int{
        let sqlite = SQLiteManager.sharedInstance
        let tmpsql = "SELECT number FROM shopping WHERE username = '\(username)' AND bookname = '\(bookname)';"
        if !sqlite.openDB(){return 99999}
        let queryres = sqlite.execQuerySQL(sql: tmpsql)
        sqlite.closeDB()
        if queryres!.count == 0{
            return 0
        }else{
            return queryres![0]["number"] as! Int
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
